package com.boss.salary.dao.impl;

import com.boss.common.util.DateUtils;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.INotInPaymentDao;
import com.boss.salary.entity.NotInPayment;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
@Repository
public class NotInPaymentDaoImpl extends BaseDaoImpl implements INotInPaymentDao {
    @Override
    public List<NotInPayment> selectNotInPayment(String mofDivCode) {
        StringBuilder sql = new StringBuilder();
        sql.append("select id, create_time, update_tme, mof_div_code, is_deleted,")
                .append(" proCatCode, proCode, payTypeCode, depBgtEcoCode, payCategoryCode ")
                .append(" from ").append(TableName.SAL_NOT_IN_PAYMENT)
                .append(" where mof_div_code=? and is_deleted=").append(SystemDictConstant.NOT_DELETED);
        Object[] params = new Object[]{
                mofDivCode
        };
        List<NotInPayment> notInPayMentList=this.getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper(NotInPayment.class),params);
        if(CollectionUtils.isEmpty(notInPayMentList)){
            return null;
        }
        return notInPayMentList;
    }

    @Override
    public void insertNotInPayment(NotInPayment notInPayMent) {
        StringBuilder sql = new StringBuilder();
        sql.append(" insert ").append(TableName.SAL_NOT_IN_PAYMENT)
            .append(" ( id, create_time, update_tme, mof_div_code, is_deleted,")
            .append(" proCatCode, proCode, payTypeCode, depBgtEcoCode, payCategoryCode) ");
        sql.append(" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        Object[] objects = new Object[] {
                SysUtils.generateGuid(),String.valueOf(DateUtils.getCurrentTimeStamp()),String.valueOf(DateUtils.getCurrentTimeStamp()),
                notInPayMent.getMof_div_code(),0,notInPayMent.getProCatCode(),notInPayMent.getProCode(),notInPayMent.getPayTypeCode(),
                notInPayMent.getDepBgtEcoCode(),notInPayMent.getPayCategoryCode()
        };
        this.getjdbcTemplate().update(sql.toString(), objects);
    }

    @Override
    public void deleteNotInPayment( NotInPayment notInPayMent) {
        StringBuilder sql = new StringBuilder();
        sql.append(" delete from  ").append(TableName.SAL_NOT_IN_PAYMENT)
               .append( " where id=? ");
        Object[] objects = new Object[] {
                notInPayMent.getId()
        };
        this.getjdbcTemplate().update(sql.toString(), objects);
    }

    @Override
    public List<NotInPayment> queryNotInPaymentPage(PaginationDTO paginationDTO, String province) {
        StringBuilder sql = new StringBuilder();
        sql.append("select id, create_time, update_tme, mof_div_code, is_deleted,")
                .append(" proCatCode, proCode, payTypeCode, depBgtEcoCode, payCategoryCode ")
                .append(" from ").append(TableName.SAL_NOT_IN_PAYMENT);
        sql.append(" where  ");
        List<Object> list = new ArrayList<>();
            sql.append("  mof_div_code = ? ");
            list.add(province);

        sql.append(" order by create_time ");
        String querySql = boxUpSql(sql.toString());
        //请求参数初始化
        Object[] params = boxUpParam(list.toArray(), paginationDTO);
        return getJdbcTemplate().query(querySql, new BeanPropertyRowMapper<>(NotInPayment.class), params);
    }

    @Override
    public int queryNotInPaymentTotal(String province) {
        StringBuilder sql = new StringBuilder();
        sql.append("select count(*) from ").append(TableName.SAL_NOT_IN_PAYMENT);
        sql.append(" where  mof_div_code = ?");
            return this.getJdbcTemplate().queryForObject(sql.toString(), Integer.class,province);

    }
}
